﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.BLL;
using MxWeiXinPF.Common;
using MxWeiXinPF.WeiXinComm;

namespace MxWeiXinPF.Web
{
    public partial class AutoExec : Page
    {
        private WeiXinCRMComm cpp = new WeiXinCRMComm();
        public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
        public static string Pwd = ConfigurationManager.AppSettings["WebReference.Service.Pwd"];
        private WeiXinSendMessageComm sm = new WeiXinSendMessageComm();
        public static string UserName = ConfigurationManager.AppSettings["WebReference.Service.UserName"];

        private void AutoComplete(DataSet ds)
        {
            orders orders = new orders();
            DataSet set = new DataSet();
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                try
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["id"].ToString()))
                        {
                            set = orders.InsertIntoDtUserRate(int.Parse(ds.Tables[i].Rows[0]["id"].ToString()));
                            if ((set != null) && (set.Tables.Count > 0))
                            {
                                if (ds.Tables[0].Rows[i]["payment_id"].ToString().Trim().Equals("1"))
                                {
                                    if (orders.UpdateField(ds.Tables[0].Rows[i]["id"].ToString().Trim(), " express_status=2,payment_status=2 , status=3,complete_time=getdate() "))
                                    {
                                        orders.WXSendMessage(int.Parse(ds.Tables[0].Rows[i]["id"].ToString().Trim()));
                                    }
                                }
                                else if (orders.UpdateField(ds.Tables[0].Rows[i]["id"].ToString().Trim(), " express_status=2,payment_status=2 , status=3,complete_time=getdate() "))
                                {
                                    orders.WXSendMessage(int.Parse(ds.Tables[0].Rows[i]["id"].ToString().Trim()));
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }

        private DataSet AutoGetComplete()
        {
            orders orders = new orders();
            DataSet set = new DataSet();
            try
            {
                return orders.GetSetComplete();
            }
            catch (Exception)
            {
                return null;
            }
        }

        private DataSet AutoGetTemplate()
        {
            sendmessage sendmessage = new sendmessage();
            DataSet set = new DataSet();
            try
            {
                return sendmessage.GetTemplateMessage();
            }
            catch (Exception)
            {
                return null;
            }
        }

        private DataSet AutoGetTSMS()
        {
            sendmessage sendmessage = new sendmessage();
            DataSet set = new DataSet();
            try
            {
                return sendmessage.GetSmsMessage();
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void AutoSetSMS(DataSet ds)
        {
            sendmessage sendmessage = new sendmessage();
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                try
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        int id = 0;
                        if (!ds.Tables[0].Rows[i]["id"].ToString().Equals(""))
                        {
                            id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                        }
                        this.SendSMSMessage(ds.Tables[0].Rows[i]["mobile"].ToString(), ds.Tables[0].Rows[i]["MessageContent"].ToString());
                        sendmessage.UpdateIsSend("1", DateTime.Now, id);
                    }
                }
                catch (Exception)
                {
                }
            }
        }

        private void AutoSetTemplate(DataSet ds)
        {
            sendmessage sendmessage = new sendmessage();
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                try
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        int id = 0;
                        if (!ds.Tables[0].Rows[i]["id"].ToString().Equals(""))
                        {
                            id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                        }
                        this.sm.SendMessageGouYaoTiXin(ds.Tables[0].Rows[i]["openid"].ToString(), 0x27, ds.Tables[0].Rows[i]["nick_name"].ToString(), ds.Tables[0].Rows[i]["MessageContent"].ToString(), ds.Tables[0].Rows[i]["productname"].ToString(), MyCommFun.getWebSite() + "/wxoauth.aspx?wid=39&p=1");
                        sendmessage.UpdateIsSend("2", DateTime.Now, id);
                    }
                }
                catch (Exception)
                {
                }
            }
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
        }

        public string GetHtmlFromUrl(string Phone, string ContentTxt)
        {
            string format = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product=&extno=";
            byte[] bytes = new UTF8Encoding().GetBytes(string.Format(format, new object[] { UserName, Pwd, Phone, ContentTxt }));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PostUrl);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Flush();
            requestStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return "1";
            }
            return "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(DateTime.Now.Hour.ToString());
            int num2 = Convert.ToInt32(DateTime.Now.Minute.ToString());
            try
            {
                DataSet ds = new DataSet();
                ds = this.AutoGetComplete();
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.AutoComplete(ds);
                }
                DataSet set2 = new DataSet();
                set2 = this.AutoGetTemplate();
                if ((set2 != null) && (set2.Tables[0].Rows.Count > 0))
                {
                    this.AutoSetTemplate(set2);
                }
                DataSet set3 = new DataSet();
                set3 = this.AutoGetTSMS();
                if ((set3 != null) && (set3.Tables[0].Rows.Count > 0))
                {
                    this.AutoSetSMS(set3);
                }
                base.Response.Write("发送成功!");
            }
            catch (Exception exception)
            {
                base.Response.Write(exception.Message);
            }
        }

        private void SendSMSMessage(string phone, string contenttxt)
        {
            string htmlFromUrl = this.GetHtmlFromUrl(phone, contenttxt);
        }
    }
}